package light.head.dao.sys;

import java.math.BigInteger;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import light.head.cache.Cache;
import light.head.constant.Parm;
import light.head.constant.Sys;
import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.framework.entity.PageBean;
import light.head.framework.utils.MD5Utils;
import light.head.model.UserAnnex;
import light.head.model.UserInfo;

import org.hibernate.HibernateException;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Service;

@Service
public class UserInfoDAO extends BaseDAO<UserInfo, Integer> {
	private OrganDAO organDAO;
	private final static String sql_findUserAnnex = " FROM UserAnnex ua WHERE ua.deleted = 0 AND ua.uc = ?";
	private final static String sql_login = "FROM UserInfo as u WHERE u.email = ? AND u.pwd = ?";
	private final static String sql_getByEmail = "FROM UserInfo as u WHERE u.email = ?";
	private final static String sql_geAu = "FROM UserInfo u WHERE u.code = ? AND u.pwd = ?";
	private final static String sql_userList = "FROM UserInfo WHERE deleted = 0 AND oc = ?";
	private final static String sql_findByOrganCode = "FROM UserInfo u WHERE u.oc = ?";
	private final static String sql_checkEmail = "FROM UserInfo u WHERE u.email = ?";
	public UserInfo login(String email, String pwd) {
		List<UserInfo> users = find(sql_login, email, pwd);
		if (null != users && users.size() > 0)
			return users.get(0);
		return null;
	}
	
	public UserInfo getByEmail(String email) {
		List<UserInfo> users = find(sql_getByEmail, email);
		if (null != users && users.size() > 0)
			return users.get(0);
		return null;
	}
	public UserInfo getAffirm(Integer code, String pwd) {
		List<UserInfo> users = find(sql_geAu, code,MD5Utils.encryptTo32(pwd));
		if (null != users && users.size() > 0)
			return users.get(0);
		return null;
	}
	
	/**
	 * 查询本机构及本机构的子机构用户的方法
	 * @author 朱修贤
	 * @date 2013-8-5
	 * @time 上午10:24:03
	 * @param map
	 * @param pageBean
	 * @return
	 */
	public RESULT findByMap(Map<String, String> map, PageBean pageBean) {
		StringBuffer hql = new StringBuffer("FROM UserInfo u WHERE u.realName <> 'admin'");
		List<Object> list = null;
		if (map != null) {
			list = new ArrayList<Object>();

			String realName = map.get(Parm.REAL_NAME);
			String isManager = map.get(Parm.ISMANAGER);
			String deleted = map.get(Parm.DELETED);
			String organCode = map.get(Parm.ORGAN_CODE);
			String oc = map.get(Parm.OC);
			String roleCode = map.get(Parm.RC);
			

			if (isNotNull(realName)) {
				hql.append(" AND u.realName LIKE ?");
				list.add("%" + realName + "%");
			}
			if (isNotNull(isManager)) {
				hql.append(" AND u.isManager = ?");
				list.add(Boolean.parseBoolean(isManager));
			}
			if (isNotNull(deleted)) {
				hql.append(" AND u.deleted = ?");
				list.add(Boolean.parseBoolean(deleted));
			}
			if (isNotNull(organCode)) {
				hql.append(" AND u.oc = ?");
				list.add(Integer.parseInt(organCode));
			}
			if (isNotNull(oc)) {
				hql.append(" AND u.oc IN (" + oc + ")");
			}
			if (isNotNull(roleCode)) {
				hql.append(" AND u.rc = ?");
				list.add(Integer.parseInt(roleCode));
			}
		}
		hql.append(" ORDER BY u.oc, u.code DESC");
		
		return findToPageBean(hql.toString(), pageBean, list);
	}
	
	public RESULT findByMap2(Map<String, String> map, PageBean pageBean, List<Integer> oc) {
		if(null == oc || oc.size() == 0)
			return RESULT.OC_NULL;
		
		StringBuffer hql = new StringBuffer("FROM UserInfo u WHERE u.realName <> 'admin' AND u.deleted = 0");
		List<Object> list = null;
		list = new ArrayList<Object>();
		
		hql.append(" AND u.oc IN (");
		for (Integer i : oc) {
			if (null != i) {
				list.add(i);
				hql.append("?,");
			}
		}
		hql.deleteCharAt(hql.length() - 1).append(")");
		
		if (map != null) {
			String realName = map.get(Parm.REAL_NAME);
			String isManager = map.get(Parm.ISMANAGER);
			String deleted = map.get(Parm.DELETED);
			String organCode = map.get(Parm.OC);
			String roleCode = map.get(Parm.RC);
			
			if (isNotNull(realName)) {
				hql.append(" AND u.realName LIKE ?");
				list.add("%" + realName + "%");
			}
			if (isNotNull(isManager)) {
				hql.append(" AND u.isManager = ?");
				list.add(Boolean.parseBoolean(isManager));
			}
			if (isNotNull(deleted)) {
				hql.append(" AND u.deleted = ?");
				list.add(Boolean.parseBoolean(deleted));
			}
			if (isNotNull(organCode)) {
				hql.append(" AND u.oc = ?");
				list.add(Integer.parseInt(organCode));
			}
			if (isNotNull(roleCode)) {
				hql.append(" AND u.rc = ?");
				list.add(Integer.parseInt(roleCode));
			}
		}
		hql.append(" ORDER BY u.oc, u.code DESC");
		
		return findToPageBean(hql.toString(), pageBean, list);
	}
	
	public RESULT update(UserInfo user) {
		if (null == user || user.getCode() == null)
			return RESULT.PARAMETER_ERROR;
		
		UserInfo old = get(user.getCode());
		if (null == old)
			return RESULT.NOT_FIND;
		
		
		if (isNotNull(user.getPwd()))
			old.setPwd(user.getPwd());
		
		if (user.getDeleted() != null)
			old.setDeleted(user.getDeleted());
		if (user.getIsManager() != null)
			old.setIsManager(user.getIsManager());
		if (isNotNull(user.getEmail()))
			old.setEmail(user.getEmail());
		if (isNotNull(user.getMobile()))
			old.setMobile(user.getMobile());
		if (isNotNull(user.getPageStyle()))
			old.setPageStyle(user.getPageStyle());
		if (isNotNull(user.getRealName()))
			old.setRealName(user.getRealName());
		if (isNotNull(user.getName()))
			old.setName(user.getName());
		if (user.getRc() != null)
			old.setRc(user.getRc());
		if (user.getQq() != null)
			old.setQq(user.getQq());
		if (user.getOc() != null)
			old.setOc(user.getOc());
		if (user.getPinyin() != null)
			old.setPinyin(user.getPinyin());
		if (user.getPy() != null)
			old.setPy(user.getPy());
		if (isNotNull(user.getCard()))
			old.setCard(user.getCard());
		if (null != user.getSex())
			old.setSex(user.getSex());
		if (null != user.getBirthday())
			old.setBirthday(user.getBirthday());
		if (null != user.getEntryTime())
			old.setEntryTime(user.getEntryTime());
		return upd(old);
	}
	
	/**
	 * 根据机构编号在缓存中获取该机构的所有用户信息，以JSON方式返回
	 * @param	机构编号
	 */
	public String userList(Integer organCode) {
		if (null == organCode)
			return null;
		
		String result = Cache.userList.get(organCode);
		if (!isNotNull(result))
			result = loadUserList(organCode);
		return result;
	}
	
	/**
	 * 加载指定机构的所有用户信息到缓存，并以JSON方式保存
	 * @param	机构编号
	 */
	public String loadUserList(Integer organCode) {
		List<UserInfo> list = find(sql_userList, organCode);
		String result = null;
		if (null != list && list.size() > 0) {
			StringBuffer sb = new StringBuffer(Sys.LB);
			for (UserInfo u : list)
				sb.append("{\"realName\":\"" + u.getRealName() + "\",\"code\":\"" + u.getCode() + "\",\"pinyin\":\"" + u.getPinyin() + "\",\"py\":\"" + u.getPy() + "\"},");
			
			sb.deleteCharAt(sb.length() - 1).append(Sys.RB);
			result = sb.toString();
			Cache.userList.put(organCode, result);
			return result;
		}
		return Sys.B;
	}
	
	/**
	 * 非渠道部人员查询渠道部用户的方法
	 * @author 朱修贤
	 * @date 2013-8-5
	 * @time 上午10:23:41
	 * @param map
	 * @param pageBean
	 * @return
	 */
	public RESULT findByMapSub(Map<String, String> map, PageBean pageBean) {
		StringBuffer hql = new StringBuffer("FROM UserInfo u WHERE u.realName <> 'admin'");
		List<Object> list = null;
		if (map != null) {
			list = new ArrayList<Object>();

			String realName = map.get(Parm.REAL_NAME);
			String isManager = map.get(Parm.ISMANAGER);
			String deleted = map.get(Parm.DELETED);
			String organCode = map.get(Parm.ORGAN_CODE);
			String oc = map.get(Parm.OC);
			String roleCode = map.get(Parm.RC);
			

			if (isNotNull(realName)) {
				hql.append(" AND u.realName LIKE ?");
				list.add("%" + realName + "%");
			}
			if (isNotNull(isManager)) {
				hql.append(" AND u.isManager = ?");
				list.add(Boolean.parseBoolean(isManager));
			}
			if (isNotNull(deleted)) {
				hql.append(" AND u.deleted = ?");
				list.add(Boolean.parseBoolean(deleted));
			}
			if (isNotNull(organCode)) {
				hql.append(" AND u.oc = (SELECT o.code FROM Organ o WHERE o.pc = ? AND o.otc = 3)");
				list.add(Integer.parseInt(organCode));
			}
			if (isNotNull(oc)) {
				hql.append(" AND u.oc = (SELECT o.code FROM Organ o WHERE o.pc = ? AND o.otc = 3)");
				list.add(Integer.parseInt(oc));
			}
			if (isNotNull(roleCode)) {
				hql.append(" AND u.rc = ?");
				list.add(Integer.parseInt(roleCode));
			}
		}
		hql.append(" ORDER BY u.oc, u.code DESC");
		
		return findToPageBean(hql.toString(), pageBean, list);
	}

	
	public List<UserInfo> findByOrganCode(Integer code) {
		List<UserInfo> list = find(sql_findByOrganCode, code);
		return list;
	}
	
	/***
	 * 根据编号查姓名
	 * @param code
	 * @return
	 */
	public String findCode(Integer code) {
		StringBuffer sql = new StringBuffer("SELECT u.realName FROM UserInfo u WHERE u.deleted = 0 AND u.code = ?");
		
		List<Object> parm = new ArrayList<Object>();
		parm.add(code);
		List<Object> objList = query(sql.toString(), null, 1, parm.toArray());	
		return (null == objList || objList.size() == 0) ? null : objList.get(0).toString();
	}
	
	/***
	 * 根据姓名查编号
	 * @param code
	 * @return
	 */
	public Integer findCode(String name) {
		StringBuffer sql = new StringBuffer("SELECT u.code FROM UserInfo u WHERE u.deleted = 0 AND u.realName = ?");
		
		List<Object> parm = new ArrayList<Object>();
		parm.add(name);
		List<Object> objList = query(sql.toString(), null, 1, parm.toArray());	
		return (null == objList || objList.size() == 0) ? null : Integer.parseInt(objList.get(0).toString());
	}

	/**
	 * 查询所有子机构的用户
	 * @author 朱修贤 @date 2013-9-29 @time 下午7:06:08
	 * @param map
	 * @param pageBean
	 * @return
	 */
	public RESULT findSubUsers(List<Integer> ocs, Map<String, String> map, PageBean pageBean) {
		StringBuffer hql = new StringBuffer("FROM UserInfo u WHERE u.realName <> 'admin' AND u.oc IN(");
		List<Object> list =  new ArrayList<Object>();
		for (Integer i : ocs) {
			hql.append("?,");
			list.add(i);
		}
		hql.deleteCharAt(hql.length() - 1).append(")");
		if (map != null) {

			String realName = map.get(Parm.REAL_NAME);
			String isManager = map.get(Parm.ISMANAGER);
			String deleted = map.get(Parm.DELETED);
			String oc = map.get(Parm.OC);
			String roleCode = map.get(Parm.RC);
			

			if (isNotNull(realName)) {
				hql.append(" AND u.realName LIKE ?");
				list.add("%" + realName + "%");
			}
			if (isNotNull(isManager)) {
				hql.append(" AND u.isManager = ?");
				list.add(Boolean.parseBoolean(isManager));
			}
			if (isNotNull(deleted)) {
				hql.append(" AND u.deleted = ?");
				list.add(Boolean.parseBoolean(deleted));
			}
			if (isNotNull(oc)) {
				hql.append(" AND u.oc IN (SELECT o.code FROM Organ o WHERE o.pc = ? or o.code = ?)");
				list.add(Integer.parseInt(oc));
				list.add(Integer.parseInt(oc));
			}
			if (isNotNull(roleCode)) {
				hql.append(" AND u.rc = ?");
				list.add(Integer.parseInt(roleCode));
			}
		}
		hql.append(" ORDER BY u.oc, u.code DESC");
		return findToPageBean(hql.toString(), pageBean, list);
	}
	
	/**
	 * 检查email是否重复(如果email为空则返回true)
	 * @param email 要检查的email
	 * @param code 用户信息编号
	 * @return 不重复返回  true, 否则返回 false
	 */
	public Boolean checkEmail(String email, Integer code) {
		if (null == email || Sys.NONE.equals(email))
			return true;
		
		List<UserInfo> list = find(sql_checkEmail, email);
		if (null == list || list.size() == 0) {
			return true;
		}
		return (null != code && list.get(0).getCode().intValue() == code.intValue()) ? true : false;
	}
	
	/**
	 * 通过角色编号集合查询所有属于oc 指定机构的用户
	 * @author YangGuangHe
	 * @Time 2013-11-17 下午1:19:27
	 * @param oc	机构编号
	 * @param codes	角色编号集合
	 * @return	返回符合条件用户信息
	 */
	public List<UserInfo> findByRoleCodes(Integer oc, List<Integer> codes) {
		if (null == oc || null == codes || codes.size() == 0)
			return null;
		
		List<Object> parms = new ArrayList<Object>();
		StringBuffer hql = new StringBuffer("FROM UserInfo u WHERE u.oc = ? AND u.rc IN (");
		
		parms.add(oc);
		
		for (Integer ig : codes) {
			hql.append("?,");
			parms.add(ig);
		}
		
		hql.deleteCharAt(hql.length() - 1).append(")");
		return find(hql.toString(), parms.toArray());
	}
	/**
	 * 通过角色编号集合查询所有属于oc 指定机构的用户
	 * @author YangGuangHe
	 * @Time 2013-11-17 下午1:19:27
	 * @param oc	机构编号
	 * @param codes	角色编号集合
	 * @return	返回符合条件用户信息
	 */
	public List<UserInfo> findByRoleRc(Integer oc, Integer rc) {
		if (null == oc || null == rc )
			return null;
		
		List<Object> parms = new ArrayList<Object>();
		StringBuffer hql = new StringBuffer("FROM UserInfo u WHERE u.oc = ? AND u.rc = ?");
		
		parms.add(oc);
		parms.add(rc);
		
		return find(hql.toString(), parms.toArray());
	}
	/**
	 * 获取机构内所有角色编号
	 */
	public List<Integer> getCuCodes(Integer organCode){
		String hql = "FROM UserInfo ui WHERE ui.oc = ?";
		return find(hql, organCode);
	}
	
	/**
	 * 获取指定机构的用户列表(完全是因为freemarker而存在)
	 * @param	机构编号
	 */
	public List<UserInfo> findByOrganCode(String organCode) {
		if (!isNotNull(organCode))
			return null;
		return getAllByOrgCode(organCode);
	}
	/**
	 * 因freemarker需使用该方法，所以参数必须使用String
	 * @param	机构编号
	 */
	public List<UserInfo> getAllByOrgCode(String code) {
		if (!isNotNull(code)) return null;
		
		Integer c = Integer.parseInt(code);
		List<UserInfo> list = Cache.userMap.get(c);
		if (null == list || list.size() == 0) {
			loadAllByOrgCode(c);
		}
		return Cache.userMap.get(c);
	}
	/**
	 * 查询当前机构及子机构的用户
	 * 因freemarker需使用该方法，所以参数必须使用String
	 * @param	code机构编号
	 */
	public List<UserInfo> getAllByOcs(String code) {
		if (!isNotNull(code)) return null;
		Integer c = Integer.parseInt(code);
		List<Integer> ocs = organDAO.getSonCodeList(c);
		
		StringBuffer hql = new StringBuffer("FROM UserInfo u WHERE u.deleted = 0");
		List<Object> list = null;
		list = new ArrayList<Object>();
		
		hql.append(" AND u.oc IN (");
		for (Integer i : ocs) {
			if (null != i) {
				list.add(i);
				hql.append("?,");
			}
		}
		hql.deleteCharAt(hql.length() - 1).append(")");
		return find(hql.toString(), list.toArray());	
	}
	/**
	 * 缓存机构对应角色列表
	 * @param c
	 */
	public RESULT loadAllByOrgCode(Integer c){
		List<UserInfo> list = find(sql_userList, c);
		Cache.userMap.put(c, list);
		return RESULT.SUCCESS;
		
	}
	
	
	public RESULT findByMapAndOc(List<Integer> ocs, Map<String, String> map, PageBean pb) {
		if (null == ocs) return null;
		List<Object> list = new ArrayList<Object>();
		StringBuffer hql = new StringBuffer(" FROM UserInfo u WHERE u.deleted = 0 AND u.oc IN (");
		
		for (Integer i : ocs) {
			if (null != i) {
				list.add(i);
				hql.append("?,");
			}
		}
		hql.deleteCharAt(hql.length() - 1).append(")");
		if (map != null) {
			String realName = map.get(Parm.REAL_NAME);
			String deleted = map.get(Parm.DELETED);
			
			if(isNotNull(realName)){
				hql.append(" AND u.realName LIKE ?");
				list.add("%" + realName + "%");
			}if (isNotNull(deleted)) {
				hql.append(" AND u.deleted = ?");
				list.add(Boolean.parseBoolean(deleted));
			}
		}
		hql.append(" ORDER BY u.code DESC");
		return findToPageBean(hql.toString(), pb, list);
	}
	
	/**
	* 根据用户编号查询用户附件信息
	* @param code
	* @return
	*/
	public List<UserAnnex> findUserAnnex(Integer code) {
		if (null == code)
			return null;
		List<UserAnnex> list = find(sql_findUserAnnex, code);
		return list;
	}
	/***
   * 根据用户编号查询申请明细
   * @param code
   * @return
   */
	public String  findName(Integer code) {
		if (null == code)
			return null;
		List<Object> list = new ArrayList<Object>();
		StringBuffer sql = new StringBuffer("SELECT u.realName FROM UserInfo u WHERE u.deleted = 0 AND u.code = ?");
		list.add(code);
		
		List<Object> objList = query(sql.toString(), null, 1, list.toArray());	
		if (null == objList || objList.size() == 0) 
			return null;
		
		return (null != objList.get(0) && !Sys.NONE.equals(objList.get(0))) ? objList.get(0).toString() : null;
	}
	/**
	* 根据机构编号查询用户信息
	* @param code
	* @return
	*/
	public List<UserInfo>  findOc(List<Integer> ocs) {
		if (null == ocs)return null;
		List<Object> list = new ArrayList<Object>();
		StringBuffer sql = new StringBuffer(" FROM UserInfo u WHERE u.deleted = 0 AND u.oc IN (");
		for (Integer i : ocs) {
			sql.append("?,");
			list.add(i);
		}
		sql.deleteCharAt(sql.length() - 1).append(")");
		
		
		return find(sql.toString(), list.toArray());	
	}
	/**
	 * 辅助查询传入机构的 全部用户
	 */
	public RESULT aidFindByMap(List<Object> ocs, Map<String, String> map, PageBean pageBean){
		if(ocs == null) return null;
		List<Object> list = new ArrayList<Object>();
		StringBuffer sql = new StringBuffer("FROM UserInfo ui WHERE ui.oc IN(");
		for(Object oc : ocs){
			sql.append("?,");
			list.add((Integer)oc);
		}
		sql.deleteCharAt(sql.length() - 1).append(")");
		if(map != null){
			String name = map.get(Parm.REAL_NAME);
			String codes = map.get(Parm.CODES);
			if(isNotNull(name)){
				sql.append(" AND ui.realName LIKE ?");
				list.add("%" + name + "%");
			}
			if(isNotNull(codes)){
				sql.append(" AND ui.code NOT IN (");
				List<Integer> codess = new ArrayList<Integer>();
				String[] orgs = codes.split(",");
				for(int i = 0; i < orgs.length; i++){
					codess.add(Integer.parseInt(orgs[i]));
				}
				for(Integer code : codess){
					sql.append("?,");
					list.add(code);
				}
				sql.deleteCharAt(sql.length() - 1).append(")");
			}
		}
		sql.append(" ORDER BY ui.code DESC");
		return findToPageBean(sql.toString(), pageBean, list);
	}
	
	/**
	 * 根据传入的codes 获取已经选择的用户信息
	 * @param codes
	 * @return
	 */
	public List<UserInfo> findUserByCodes(String codes){
		if(codes != null){
			StringBuffer sb = new StringBuffer("FROM UserInfo ui WHERE ui.code IN ( ");
			List<Integer> codess = new ArrayList<Integer>();
			String[] orgs = codes.split(",");
			for(int i = 0; i < orgs.length; i++){
				codess.add(Integer.parseInt(orgs[i]));
			}
			for(Integer code : codess){
				sb.append(code + ",");
			}
			sb.deleteCharAt(sb.length() - 1).append(")");
			final String hql = sb.toString();
			return find(hql);//return find(hql,null);
		}
		return null;
	}
	
	/**
	 * 获取富父机构及自己够的全部用户数量
	 * 返回一个Integer 类型的数量
	 */
	public Integer findUserCountByCodes(List<Integer> ocs){
		if(ocs == null){
			return null;
		}
		StringBuffer sb = new StringBuffer();
		List<Object> list = new ArrayList<Object>();
		sb.append("SELECT COUNT(*) FROM UserInfo ui WHERE ui.oc IN (");
		for(Integer oc : ocs){
			sb.append("?,");
			list.add(oc);
		}
		sb.deleteCharAt(sb.length() - 1).append(" )");
		sb.append(" AND ui.deleted = 0 ");
		final String sql = sb.toString();
		final List<Object> params = list;
		return this.getHibernateTemplate().execute( new HibernateCallback<Integer>() {
			@Override
			public Integer doInHibernate(Session session)throws HibernateException, SQLException {
				SQLQuery query = session.createSQLQuery(sql);
				if (params != null) {
					for (int i = 0; i < params.size(); i++) {
						query.setParameter(i, params.get(i));
					}
				}
				@SuppressWarnings("unchecked")
				List<Object> resultList = query.list();
				Integer count = 0;
				if (resultList != null) {
					for (Object row : resultList) {
						count = row != null ? ((BigInteger)row).intValue() : 0;
					}
					return count;
				}
				return null;
			}
		});		
	}
	public RESULT findByMapOc(List<Integer> ocs, Map<String, String> map, PageBean pageBean){
		if(ocs == null) return null;
		List<Object> list = new ArrayList<Object>();
		StringBuffer sql = new StringBuffer("FROM UserInfo c WHERE c.oc IN (");
		for (Integer i : ocs) {
			sql.append("?,");
			list.add(i);
		}
		sql.deleteCharAt(sql.length() - 1).append(")");
		if(map != null){
			String realName = map.get(Parm.REAL_NAME);
			String on = map.get(Parm.ON);
			
			if(isNotNull(realName)){
				sql.append(" AND c.realName LIKE ?");
				list.add("%" + realName + "%");
			}
			if(isNotNull(on)){
				sql.append(" AND c.on LIKE ?");
				list.add("%" + on + "%");
			}
		}
		sql.append(" ORDER BY c.code DESC");
		return findToPageBean(sql.toString(), pageBean, list);
	}
	
	//根据用户编号和机构编号查询角色编号
	public Integer findByOc(Integer cu){
		if(cu == null ) return null;
		StringBuffer sql = new StringBuffer("SELECT u.rc FROM UserInfo u WHERE u.deleted = 0 AND u.code = ? ");
		List<Object> parm = new ArrayList<Object>();
		parm.add(cu);
		List<Object> objList = query(sql.toString(), null, 1, parm.toArray());	// 获取审核状态
		return (null == objList || objList.size() == 0) ? null : Integer.parseInt(objList.get(0).toString());
	}
	//根据用户编号和机构编号查询角色编号
		public Integer findByOc(Integer cu,Integer oc){
			if(cu == null ) return null;
			StringBuffer sql = new StringBuffer("SELECT u.rc FROM UserInfo u WHERE u.deleted = 0 AND u.code = ? AND u.oc = ?");
			List<Object> parm = new ArrayList<Object>();
			parm.add(cu);
			parm.add(oc);
			List<Object> objList = query(sql.toString(), null, 1, parm.toArray());	// 获取审核状态
			return (null == objList || objList.size() == 0) ? null : Integer.parseInt(objList.get(0).toString());
		}
		
		//根据用户编号查询角色编号
		public Integer findByRc(Integer cu){
			if(cu == null ) return null;
			StringBuffer sql = new StringBuffer("SELECT u.rc FROM UserInfo u WHERE u.deleted = 0 AND u.code = ? ");
			List<Object> parm = new ArrayList<Object>();
			parm.add(cu);
			List<Object> objList = query(sql.toString(), null, 1, parm.toArray());	
			return (null == objList || objList.size() == 0) ? null : Integer.parseInt(objList.get(0).toString());
		}
		
	@Autowired
	public void setOrganDAO(OrganDAO organDAO) {
		this.organDAO = organDAO;
	}
	
}
